#
# Great Statesman Mod
# CvGreatStatesmanEventManager
# 

from CvPythonExtensions import *
import CvUtil

import CvEventManager
import sys
import PyHelpers
import CvMainInterface
import CvActionButtonUtils
import CvConfigParser

gc = CyGlobalContext()
localText = CyTranslator()
PyPlayer = PyHelpers.PyPlayer
PyInfo = PyHelpers.PyInfo

# Increase or decrease this value to change the amount great statesmen improve 
# diplomatic relations between their civilization and another civilization.
# Default value is 5
g_iDiplomaticRelationsImprovementAmount = 5

# Change this to have the legal civic option bureaucracy give free statemen to 
# civilizations that have the legal civic option bureaucracy set. The free 
# statesmen are added to the civilization's capital city.
# Default value is true
g_bBureaucracyCivicAddsStatesmen = true

# Change this to increase or decrease the number of free statesmen that are 
# added to a civilization's capital city if they select bureaucracy as their 
# legal civic option.
# Default value is 2
g_iBureaucracyStatesmanBonus = 2

# globals
###################################################
class CvGreatStatesmanEventManager:
	def __init__(self, eventManager):
		# initialize base class

		eventDefinition = ('DiplomaticsRelationsPopupEvent', self.__eventDiplomaticsRelationsPopupApply, self.__eventDiplomaticsRelationsPopupBegin)

		eventManager.addCustomEventDefinition(CvUtil.DIPLOMATIC_RELATIONS_POPUP, eventDefinition)
		
		global g_iDiplomaticRelationsImprovementAmount
		global g_bBureaucracyCivicAddsStatesmen
		global g_iBureaucracyStatesmanBonus 

		config = CvConfigParser.CvConfigParser("ViSa_v3.ini")
		
		# Read the configuration values for the great statesman
		if(config != None):
			g_iDiplomaticRelationsImprovementAmount = config.getint("Great Statesman", "Diplomatic Relations Improvement Amount", 5)
			g_bBureaucracyCivicAddsStatesmen = config.getboolean("Great Statesman", "Bureaucracy Civic Adds Statesmen", true)
			g_iBureaucracyStatesmanBonus = config.getint("Great Statesman", "Bureaucracy Statesman Bonus", 2)
			
		eventManager.addEventHandler("BeginPlayerTurn", self.onBeginPlayerTurn)

		eventManager.addEventHandler("mouseEvent", self.onMouseEvent)


	def onMouseEvent(self, argsList):
		'mouse handler - returns 1 if the event was consumed'
		eventType,mx,my,px,py,interfaceConsumed,screens = argsList
		

		#gc.getPlayer(0).getCapitalCity().createGreatPeople(gc.getInfoTypeForString("UNIT_STATESMAN"), false, false)

	def onBeginPlayerTurn(self, argsList):
		'Called at the beginning of a players turn'
		iGameTurn, iPlayer = argsList

		if(iPlayer < 0):
			return
		
		# Get the playe
		objPlayer = gc.getPlayer(iPlayer)
		
		# Get the player's capital city
		objCity = objPlayer.getCapitalCity()
		
		iGreatStatesmanID = gc.getInfoTypeForString("SPECIALIST_STATESMAN")
		iGreatStatesmanCount = objCity.getFreeSpecialistCount(iGreatStatesmanID)
		
		legalCivicType = objPlayer.getCivics(gc.getInfoTypeForString("CIVICOPTION_LEGAL"))

		if(legalCivicType == gc.getInfoTypeForString("CIVIC_BUREAUCRACY") and g_bBureaucracyCivicAddsStatesmen):
			if(iGreatStatesmanCount == 0):
				objCity.changeFreeSpecialistCount(iGreatStatesmanID, g_iBureaucracyStatesmanBonus)
		else:
			if(iGreatStatesmanCount >= 2):

				objCity.changeFreeSpecialistCount(iGreatStatesmanID, (g_iBureaucracyStatesmanBonus*-1))

	def __eventDiplomaticsRelationsPopupBegin(self):
		pass
		   
		   
	def __eventDiplomaticsRelationsPopupApply(self, playerID, netUserData, popupReturn):
		pPlayer = gc.getActivePlayer()
		iPlayer = pPlayer.getID()

		targetPlayer = gc.getPlayer(popupReturn.getButtonClicked())
		iTargetPlayer = gc.getPlayer(targetPlayer.getID())
		
		# Return immediately if the value returned for the button clicked
		# wasn't for a player in the game
		if(targetPlayer == None):
			return

		# If the player clicked is at war with the current player then force 
		# peace treaty between the two, otherwise increase their relationship
		if(gc.getTeam(iTargetPlayer.getTeam()).isAtWar(pPlayer.getTeam())):
			gc.getTeam(pPlayer.getTeam()).makePeace(iTargetPlayer.getTeam())
		else:
			targetPlayer.AI_changeAttitudeExtra(iPlayer, g_iDiplomaticRelationsImprovementAmount)

		CyInterface().setDirty(InterfaceDirtyBits.Score_DIRTY_BIT, True)

		
		# Get all of the statesmen the player has
		greatStatesmenList = PyPlayer(iPlayer).getUnitsOfType(gc.getInfoTypeForString("UNIT_STATESMAN"))
		
		# Go through all of the statesmen
		for unit in greatStatesmenList:		
		
			# If we find one that is selected then kill it off and break out of
			# the loop
			if(unit.IsSelected()):
				unit.kill(False, 1)
				CyInterface().setDirty(InterfaceDirtyBits.PlotListButtons_DIRTY_BIT, True)
				break	
